package com.wuyixu.service.impl;

import java.util.List;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wuyixu.dao.SpecMapper;
import com.wuyixu.domain.Spec;
import com.wuyixu.domain.SpecOption;
import com.wuyixu.service.SpecService;


@Service
public class SpecServiceImpl implements SpecService{
	@Autowired
	private SpecMapper specMapper;
	@Override
	public PageInfo<Spec> selects(Spec spec, Integer pageNum, Integer pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<Spec> list=specMapper.selects(spec);
		return new PageInfo<Spec>(list);
	}

	@Override
	public int add(Spec spec) {
		int result=specMapper.addSpec(spec);
		List<SpecOption> list = spec.getOptions();
		for (SpecOption option : list) {
			option.setSpecId(spec.getId());
			result+=specMapper.addOption(option);
		}
		return result;
	}

	@Override
	public int delete(int[] ids) {
		// TODO Auto-generated method stub
		int result = specMapper.delOptions(ids);
		result+=specMapper.delSpec(ids);
		
		return result;
	}

	@Override
	public int update(Spec spec) {
		// TODO Auto-generated method stub
		int result = specMapper.update(spec);
		//删除子表
		specMapper.delOptions(spec.getId());
		
		//重新插入子表
		List<SpecOption> options = spec.getOptions();
		for (SpecOption specOption : options) {
			//设置主键
			specOption.setSpecId(spec.getId());
			result+=specMapper.addOption(specOption);
		}
		
		return result;
	}

	@Override
	public Spec getById(int id) {
		// TODO Auto-generated method stub
		return specMapper.findById(id);
	}

	@Override
	public List<Spec> listAll() {
		// TODO Auto-generated method stub
		return specMapper.listAll();
	}
	
}
